org   0x8000

;寄存器空间：
;8ff0H - 8fffH      临时变量
;9000H - 90feH      输入队列
;90ffH              输入队列指针

entry:
    mov     ax, 0
    mov     ss, ax
    mov     ds, ax
    mov     es, ax

message:
    mov     si, msg
    call    print
    mov     si, msg3
    call    print

timeloop:
    mov     al,00H
    MOV     ah, 00H
    INT     16H
    cmp     al, 00H
    je      timeloop    ;无输入直接跳过

    ;有输入则进行存储并检测
    mov     ebx,90ffH
    mov     ecx,9000H

    cmp     al, 0dh     ;输入为回车直接跳转
    je      wegetit

    add     cl, byte [ebx]
    mov     byte [ecx],al
    add     byte [ebx],01H

    MOV     cx, 01H     ;控制字符显示重复一次
    MOV     ah, 0eH
    int     10H
    ; MOV     ah, 09H
    ; int     10H

    ; cmp     al, 't'
    ; je      seetime
    jmp     timeloop

wegetit:
    mov     al, 0dh
    int     10H
    ;指针重置
    mov     ebx,90ffH
    mov     byte [ebx],00H

    ;比较信息
    mov     ecx,9000H
    cmp     byte[ecx],"1"
    je      seetime
    cmp     byte[ecx],"2"
    je      fixhour
    cmp     byte[ecx],"3"
    je      fixday

    mov     si, msg4
    call    print
    mov     si, msg3
    call    print
    jmp     timeloop

fixhour:
    ;高位BCD转化
    call ascii_to_bcd

    mov ah, 02H
    int 1AH
    mov ah, 03H
    mov ch, al
    int 1AH
    call seetime

    mov     si, msg3
    call    print
    jmp     timeloop

fixday:
    call ascii_to_bcd

    mov ah, 04H
    int 1AH
    mov ah, 05H
    mov dl, al
    int 1AH
    call seetime

    mov     si, msg3
    call    print
    jmp     timeloop

seetime:
    mov     bl,00H      ;清空缓存
    mov     bh,00H      ;清空缓存
    
    mov     ah, 0x0e 
    mov     al,'='
    int     10H
    mov     al,'='
    int     10H

    mov     ah,04H
    int     1AH
    mov     al,ch       ;世纪
    call    bcd_to_ascii_null
    mov     al,cl       ;year
    call    bcd_to_ascii
    mov     al,dh       ;m
    call    bcd_to_ascii
    mov     al,dl       ;d
    call    bcd_to_ascii_space

    mov     ah,02H
    int     1AH
    mov     al,ch       ;hour
    call    bcd_to_ascii
    mov     al,cl       ;min
    call    bcd_to_ascii
    mov     al,dh       ;sec
    call    bcd_to_ascii_null

    mov     al,0dh
    int     10H
    mov     al,0ah
    int     10H

    mov     si, msg3
    call    print
    jmp     timeloop      
ret

bcd_to_ascii:            ;BCD码转ASCII,后缀冒号
    mov     ah, 0x0e     ;打字机模式
    mov     ebx,8fffH
    mov     byte [ebx], al
    ; mov     bh,al
    shr     al,4
    and     al,0x0f
    add     al,0x30
    int     0x10

    MOV     al,[ebx]
    and     al,0x0f                        
    add     al,0x30
    int     0x10

    mov     al,":"
    int     0x10
ret

bcd_to_ascii_space:            ;BCD码转ASCII，后缀空格
    mov     ah, 0x0e     ;打字机模式
    mov     ebx,8fffH
    mov     byte [ebx], al
    ; mov     bh,al
    shr     al,4
    and     al,0x0f
    add     al,0x30
    int     0x10

    MOV     al,[ebx]
    and     al,0x0f                        
    add     al,0x30
    int     0x10

    mov     al," "
    int     0x10
ret

bcd_to_ascii_null:            ;BCD码转ASCII，无后缀
    mov     ah, 0x0e     ;打字机模式
    mov     ebx,8fffH
    mov     byte [ebx], al
    ; mov     bh,al
    shr     al,4
    and     al,0x0f
    add     al,0x30
    int     0x10

    MOV     al,[ebx]
    and     al,0x0f                        
    add     al,0x30
    int     0x10
ret

ascii_to_bcd:            ;ASCII码转BCD,存入al
    ;高位BCD转化
    mov     ecx,9002H
    sub     byte [ecx],0x30
    rol     byte [ecx],4
    and     byte [ecx],11110000B      ;取得高四位
    mov     al, byte [ecx]

    mov     ecx,9003H
    sub     byte [ecx],0x30
    and     byte [ecx],0fH      ;取得低四位
    add     al, byte [ecx]
ret

print:
putloop:
    mov     al, [si]
    add     si, 1
    cmp     al, 0
    je      quitputloop
    mov     ah, 0x0e
    mov     bx, 15
    int     0x10
    jmp     putloop
quitputloop:
ret

jmp $

msg:
    DB   "::::::::::::SDZhao's test OS::::::::::::",0dh,0ah
    DB   "-------------version:1.0.0--------------",0dh,0ah
    DB   "---------Researcher:Shengda Zhao--------",0dh,0ah
    DB   "-----------student ID:17271113----------",0dh,0ah,0dh,0ah
    DB   "For use in the computer operating system course of Beijing Jiaotong University.",0dh,0ah
    DB   "Please input 1 to see sys time.",0dh,0ah
    DB   "Please input 2-XX to reset sys time hour(XX).",0dh,0ah
    DB   "Please input 3-XX to reset sys time day(XX).",0dh,0ah,00h
    DB   00h
msg2:
    DB   "test msg2",0dh,0ah,00h
msg3:
    DB   0dh,0ah,"ZSD:",00h
msg4:
    DB   0dh,0ah,"WRONG INPUT",00h

times 1024-($-$$) db 0